package com.gxa.controller;

import com.gxa.pojo.Duty;
import com.gxa.service.DutyService;
import com.gxa.service.LogService;
import com.gxa.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/duty")
@CrossOrigin
public class DutyController {

    @Resource
    DutyService dutyService;
    @Autowired
    LogService logService;

    JsonResult jsonResult;

    @ResponseBody
    @RequestMapping("/dutyPage")
    public Map findDutyByPage(@RequestBody Map map){
        String number=map.get("number").toString();
        String size=map.get("size").toString();
        Integer offset= (Integer.valueOf(number)-1)*Integer.parseInt(size);
        Integer limit= Integer.valueOf(size);
        System.out.println("page:"+offset);
        System.out.println("limit:"+limit);
        List<Duty> dutyByPage = dutyService.findDutyByPage(offset, limit);
        Integer count = dutyService.findAllDuty();
        jsonResult=new JsonResult();
        jsonResult.setData(dutyByPage);
        jsonResult.put("count",count);
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/findDutyAll")
    public List<Duty> findAll(){
        return dutyService.findAll();
    }

    @ResponseBody
    @RequestMapping("/dutyAdd")
    public Map insertDuty(@RequestBody Map map){
        jsonResult=new JsonResult();
        String name = map.get("name").toString();
        String dutyByName = dutyService.findDutyByName(name);
        if (dutyByName!=null){
            jsonResult.setCode("401");
            jsonResult.setMsg("该职位已存在！");
            return jsonResult.getValues();
        }
        Date date=new Date();
//        String time=map.get("addTime").toString();
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        ParsePosition pos = new ParsePosition(0);
//        Date date=dateFormat.parse(time,pos);
        map.put("addTime",date);
        System.out.println(map);
        Map log = (Map) (map.get("log"));
        log.put("operationTime",date);
        int i = logService.insertLog(log);
        System.out.println("map:"+toString());
        Integer n=dutyService.insertDuty(map);
        if (n>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("添加成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开小差！！");
        }
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/dutyDel")
    public Map deleteDutyById(@RequestBody Map map){
        jsonResult =new JsonResult();
        System.out.println(map);
        Date date=new Date();
        Map log = (Map) (map.get("log"));
        log.put("operationTime",date);
        int i = logService.insertLog(log);
        String id = map.get("dutyID").toString();
        Integer n = dutyService.deleteDutyById(Integer.valueOf(id));
        if (n>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开小差！！");
        }
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/dutyEdit")
    public Map updateDuty(@RequestBody Map map){
        jsonResult=new JsonResult();
        System.out.println(map);
        Date date=new Date();
        Map log = (Map) (map.get("log"));
        log.put("operationTime",date);
        int i = logService.insertLog(log);
        Integer n=dutyService.updateDutyById(map);
        if (n>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("修改成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开小差！！");
        }
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/dutyState")
    public Map updateDutyState(@RequestBody Map map){
        jsonResult=new JsonResult();
        System.out.println(map);
        Date date=new Date();
        Map log = (Map) (map.get("log"));
        log.put("operationTime",date);
        int i = logService.insertLog(log);
        Integer n = dutyService.updateDutyState(map);
        if (n>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("状态修改成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开小差！！");
        }
        return jsonResult.getValues();
    }
}